package com.nj9you.sdk.model;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.nj9you.sdk.utils.Utils;

/**
 * sdk相关参数
 */
public class SdkInfo extends DeviceInfo {
    private Context mContext;
    private static SdkInfo sSdkInfo;
    private long loginExpireTime = 24 * 3600 * 1000 * 360;
    private String game;

    public void init(String appId, String channel) {
        setDeviceInfo(appId, channel);
    }

    private SdkInfo(Context context) {
        mContext = context.getApplicationContext();
    }

    public static SdkInfo get(Context context) {
        if (sSdkInfo == null) {
            sSdkInfo = new SdkInfo(context);
        }
        return sSdkInfo;
    }

    private void setDeviceInfo(String appId, String channel) {
        setAppId(appId);
        setChannel(channel);
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String simOperatorName = tm.getSubscriberId();
        String phoneNumber = tm.getLine1Number();
        String imei = tm.getDeviceId();
        setImei(imei);
        setIccid(tm.getSimSerialNumber());
        setImsi(simOperatorName);
        setModel(android.os.Build.MODEL);
        String os = android.os.Build.VERSION.RELEASE;
        setOs(os);
        setCpu(Utils.getCpuName());
        setHz(Utils.getMaxCpuFreq());
        setRam(Utils.formatSize(Utils.getTotalMemory()));
        setRom(Utils.formatSize(Utils.getTotalInternalMemorySize()));
        setNetType(Utils.getNetworkType(mContext));
        if (simOperatorName != null) {
            String ProvidersName = "";
            if (simOperatorName.startsWith("46000") || simOperatorName.startsWith("46002")) {
                ProvidersName = "中国移动";
            } else if (simOperatorName.startsWith("46001")) {
                ProvidersName = "中国联通";
            } else if (simOperatorName.startsWith("46003")) {
                ProvidersName = "中国电信";
            }
            setProvidersName(ProvidersName);
        }
        if (!TextUtils.isEmpty(phoneNumber)) {
            if (phoneNumber.contains("+86")) {
                phoneNumber = phoneNumber.substring(3);
            }
        }
        setPhoneNum(phoneNumber);
        setMade(android.os.Build.MANUFACTURER);
        /*
        if (AndroidUtil.getRootAhth()) {
            setRoot("1");
        } else {
            setRoot("0");
        }*/
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();

        int width = dm.widthPixels;
        int height = dm.heightPixels;
        setDisplay(width + "," + height);
    }

    public long getLoginExpireTime() {
        return loginExpireTime;
    }

    public String getGame() {
        return game;
    }
}
